import Cookies from 'js-cookie';
import {getRanking} from "../../api/rankingApi";
import {getCollage} from "../../api/studentApi";
import {getMajor} from "../../api/studentApi";
import {getClass} from "../../api/studentApi";
import loginStore from "../../store/modules/login.js"

const state = {
    student_info:Cookies.get('student_info')===undefined?null:JSON.parse(Cookies.get('student_info')),
    rank_list:[],
    rank_num:[],
    rank_list_total_page:1,
    collage_list:[],
    major_list:[],
    col_maj_cla_list:[],
    rankData:{
        nowPage:1,
        type:'1',
        colId: '',
        majorId: '',
        classId:'',
        offset:'',
        limit:'',
    },
    studentRank :{
        stuId : '',
        stuRank : '未选中',
    }
}

const mutations = {

    SET_RANK_LIST: (state, rank_list)=>{
        state.rank_list = rank_list,
       state.rank_list.forEach((item,index) =>{
           item.rank=(state.rankData.nowPage-1)*7+index+1;
           item.key=index
       })
    },

    SET_RANK_LIST_TOTAL_PAGE: (state, rank_list_total_page)=>{
        state.rank_list_total_page = rank_list_total_page;
    },
    SET_COLLAGE:(state,collage_list)=>{
      state.collage_list=[]
        collage_list.forEach(item=>{
            state.collage_list.push({
                value:item.colId,
                label:item.name,
                children:[{
                    value:'error',
                    label:'专业获取失败',
                    children:[{
                        value:'error',
                        label:'班级获取失败'
                    }]
                }]
            })
        });

    },
    SET_COLLAGE_MAJOR:(state, data)=>{
        const {major_list, colId} = data;
        state.collage_list.forEach(item=>{
            if(String(item.value)===String(colId)){
                item.children=[];
                major_list.forEach(maj_item=>{
                    item.children.unshift({
                        value:maj_item.majorId,
                        label:maj_item.name,
                        children:[{
                            value:'error',
                            label:'班级获取失败'
                        }]
                    })
                })
            }
        })
    },
    SET_COLLAGE_MAJOR_CLASS:(state, data)=>{
        const {class_list, majorId} = data;
        state.collage_list.forEach(item=>{
            item.children.forEach(calRes=>{
                if(String(calRes.value)===String(majorId)){
                    calRes.children=[];
                    class_list.forEach(cla_item=>{
                        calRes.children.unshift({
                            value:cla_item.claId,
                            label:cla_item.claNumber+"班"
                        })
                    })
                }
            })
        })
    },
    NULL:()=> {}
}

const actions = {

    getRanking({commit,state},page) { //获取排名
        state.rankData.nowPage = page;
        const reqData = {
            type:Number(state.rankData.type===''?null:state.rankData.type),
            colId: state.rankData.colId===''?null:state.rankData.colId,
            majorId: state.rankData.majorId===''?null:state.rankData.majorId,
            classId:state.rankData.classId===''?null:state.rankData.classId,
            offset:((page<1?1:(page)) - 1) * 7,
            limit:7,
            studentID:loginStore.state.student_id,
            nowPage:page,
        }
        state.rankData.nowPage = page
        return new Promise((resolve, reject) => {
            getRanking(reqData).then(res=>{
                if (res.code === 1) {
                    const repdata = res.data;
                        commit('SET_RANK_LIST', repdata.allStudent);
                        commit('SET_RANK_LIST_TOTAL_PAGE', repdata.totalPage);
                        const studentRank = {
                            stuId : loginStore.state.student_id,
                            stuRank : repdata.myRank
                        };
                        state.studentRank = studentRank
                    resolve(res);
                }else {
                    this.$message.error('网络异常')
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    // getStudentRank({state}){
    //     const loginState =  loginStore.state;
    //     const studentRank = {
    //         stuId : loginState.student_id,
    //         stuRank : ''
    //     }
    //     console.log(studentRank)
    //     state.rank_list.forEach(item=>{
    //         if(String(item.stuID)===String(studentRank.stuId)){
    //             studentRank.stuRank = item.rank
    //             state.studentRank = studentRank
    //         }
    //     })
    // },

    getCollage({commit}) {
        return new Promise((resolve, reject) => {
            getCollage().then(res => {
                if (res.code === 1) {
                    commit('SET_COLLAGE', res.data);
                }else {
                    this.$message.error('网络异常')
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getMajor({commit},col_id) {
        return new Promise((resolve, reject) => {
            getMajor({collageID:col_id}).then(res => {
                const data = {
                    colId: col_id,
                    major_list: res.data,
                }
                if (res.code === 1) {
                    commit('SET_COLLAGE_MAJOR', data);

                }else {
                    resolve(res);
                }
                resolve(res);
            }).catch(error => {
                reject(error)
            })
        })
    },
    getClass({commit},maj_id){
        return new Promise((resolve,reject)=>{
            getClass({majorID:maj_id}).then(res =>{
                const data ={
                    majorId:maj_id,
                    class_list:res.data,
                }
                if (res.code === 1){
                    commit('SET_COLLAGE_MAJOR_CLASS',data);
                }else {
                    resolve(res);
                }
                resolve(res);
            }).catch(error =>{
                reject(error)
            })
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
